import { _decorator, Component, EventTouch, Input, instantiate, Node, UITransform, Vec2, Vec3 } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { GAME_POP, GAME_PREFAB, GlobalData, SOUND_TYPE } from '../../Data/GlobalData';
import { SingleLine } from './SingleLine/SingleLine';
import { SingleMajiang } from './SingleLine/SingleMajiang';
const { ccclass, property } = _decorator;

@ccclass('MajiangLoad')
export class MajiangLoad extends BaseComponent {

    //单线数组
    public MajiangLineArray: Node[] = [];

    //其实创建数量
    public MajiangLineCount: number = 7;

    //
    private LineInfo: number[] = [1, 1, 1, 0, 0, 0, 2]

    //发牌按钮
    @property(Node)
    private SendBtn: Node = null;

    //点击层
    @property(Node)
    private TouchNode: Node = null;

    //第一个点击
    private FirstClick: Node = null;

    //最大值
    private MaxNum: number = 2;

    protected Init(): void {
        for (let i = 0; i < this.MajiangLineCount; i++) {
            let sl: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.SingleLine]);
            this.node.addChild(sl);
            sl.getComponent(SingleLine).MajiangLoad = this;
            this.MajiangLineArray.push(sl);
        }

        this.TouchNode.on(Input.EventType.TOUCH_START, this.TouchStart, this);

        GlobalData.SendPos = this.SendBtn.getWorldPosition();

    }

    /**
     * 点击屏幕
     */
    private TouchStart(_e: EventTouch) {
        let click_pos: Vec2 = _e.getUILocation();
        let click_pos_world: Vec3 = this.node.getComponent(UITransform).convertToWorldSpaceAR(new Vec3(click_pos.x, click_pos.y, 0));


        for (let sl of this.MajiangLineArray) {
            let sl_pos: Vec3 = this.node.getComponent(UITransform).convertToWorldSpaceAR(sl.getWorldPosition());
            if (Math.abs(sl_pos.y - click_pos_world.y) <= 50) {

                if (!sl.getComponent(SingleLine).LockLoad.active) {

                    if (this.FirstClick) {
                        if (this.FirstClick == sl) {
                            app.audioManager.PlayEffect(SOUND_TYPE.Boom);
                            this.FirstClick.getComponent(SingleLine).IamReset();
                        } else {
                            //判断是否能合成
                            if (sl.getComponent(SingleLine).ReturnLastIndex() == this.FirstClick.getComponent(SingleLine).ClickIndex ||
                                sl.getComponent(SingleLine).ShowMajiang.length == 0) {
                                app.audioManager.PlayEffect(SOUND_TYPE.Boom);
                                sl.getComponent(SingleLine).AddClickMajiang(this.FirstClick.getComponent(SingleLine));
                            } else {

                                app.audioManager.PlayEffect(SOUND_TYPE.Wrong);
                                this.FirstClick.getComponent(SingleLine).IamWrong();
                            }
                        }
                        this.FirstClick = null;
                    } else {
                        if (sl.getComponent(SingleLine).ReturnCanClick()) {
                            app.audioManager.PlayEffect(SOUND_TYPE.Boom);
                            this.FirstClick = sl;
                            sl.getComponent(SingleLine).IamChoose();
                        }
                    }

                }

            }
        }

    }

    protected OnShow(): void {
        this.MaxNum = 2;
        for (let i = 0; i < this.MajiangLineArray.length; i++) {
            this.MajiangLineArray[i].getComponent(SingleLine).Init(this.LineInfo[i]);
        }

        for (let sl of this.MajiangLineArray) {
            if (!sl.getComponent(SingleLine).LockLoad.active) {
                let ran_num: number = 1 + Math.floor(Math.random() * 3);
                for (let i = 0; i < ran_num; i++) {
                    let ma_index: number = Math.floor(Math.random() * 2);
                    sl.getComponent(SingleLine).AddMajiang(GlobalData.SendPos, ma_index);
                }
            }
        }

    }

    /**
     * 点击发牌
     */
    private SendClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);

        if (this.FirstClick) {
            this.FirstClick.getComponent(SingleLine).IamReset();
            this.FirstClick = null;
        }


        for (let sl of this.MajiangLineArray) {
            if (!sl.getComponent(SingleLine).LockLoad.active) {
                let ran_num: number = Math.floor(Math.random() * 3);
                for (let i = 0; i < ran_num; i++) {
                    let ma_index: number = Math.floor(Math.random() * this.MaxNum);
                    sl.getComponent(SingleLine).AddMajiang(GlobalData.SendPos, ma_index);
                }
            }
        }

    }

    /**
     * 判断游戏结束
     */
    public CheckGameOver() {

        //解锁的
        let unlock_node: Node[] = [];

        for (let sl of this.MajiangLineArray) {
            if (!sl.getComponent(SingleLine).LockLoad.active) unlock_node.push(sl);
        }

        for (let un of unlock_node) {
            if (un.getComponent(SingleLine).ShowMajiang.length < un.getComponent(SingleLine).Majiang.children.length) {
                return;
            }
        }

        console.log('游戏结束')
        app.popManager.ShowPop(GAME_POP.PopEnd);

    }

    /**
     * 更新最大数
     */
    public UpdateMax(_clearindex: number, _sl: Node) {

        if (_clearindex > 9) {
            console.log("游戏结束");
            return;
        }

        if (_clearindex > this.MaxNum) this.MaxNum = _clearindex;

        switch (_clearindex) {
            case 2:
                this.MajiangLineArray[2].getComponent(SingleLine).LockLoad.active = false;
                break;
            case 4:
                this.MajiangLineArray[1].getComponent(SingleLine).LockLoad.active = false;
                break;
            case 6:
                this.MajiangLineArray[0].getComponent(SingleLine).LockLoad.active = false;
                break;
        }

        for (let i = 0; i < 2; i++)_sl.getComponent(SingleLine).AddMajiang(GlobalData.SendPos, _clearindex);
    }



}


